<template>
  <div class="box">
    <div class="shell">
      <div class="container a-container" ref="aContainer">

        <!-- 用户注册表单 -->
        <el-form :model="ruleForm" id="a-form" status-icon :rules="rules" ref="ruleForm" label-width="100px"
          class="demo-ruleForm form" size="">

          <el-form-item label="身份" prop="use_role" class="inputs">
            <el-radio v-model="ruleForm.use_role" label="ROLE_student" @change="teacher = true">学生</el-radio>
            <el-radio v-model="ruleForm.use_role" label="ROLE_teacher" @change="teacher = false">教师</el-radio>
          </el-form-item>

          <el-form-item label="学号" prop="username" v-if="teacher" class="inputs">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>

          <el-form-item label="工 号" prop="username" v-else class="inputs">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>

          <el-form-item label="姓名" prop="nickname" class="inputs">
            <el-input v-model="ruleForm.nickname"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" class="inputs">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone" class="inputs">
            <el-input v-model.number="ruleForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="学院" prop="departmentId" class="inputs" size="medium">
            <el-select style="width: 100%;" v-model="ruleForm.departmentId" placeholder="请选择学院" @change="majors()">
              <el-option v-for="item in department" :label="item.departmentName" :value="item.departmentId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="专业" prop="majorId" v-if="teacher" class="inputs" size="medium">
            <el-select style="width: 100%;" v-model="ruleForm.majorId" placeholder="请选择专业">
              <el-option v-for="item in major" :label="item.majorName" :value="item.majorId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="">
            <el-button type="primary" class="form_button button submit" style="margin: 0;"
              @click="submitForm('ruleForm')">注册</el-button>
            <el-button @click="resetForm('ruleForm')" class="resetting">重置</el-button>
          </el-form-item>
        </el-form>

      </div>

      <!-- 登录表单 -->
      <div class="container b-container" ref="bContainer">
        <el-form action="" method="" class="form" id="b-form" ref="loginForm" :model="user" auto-complete="on"
          label-position="left">
          <h2 class="form_title title">登入账号</h2>
          <input type="text" class="form_input" v-model="user.username" placeholder="请输入学号或工号">
          <input type="password" class="form_input" v-model="user.password" placeholder="请输入密码">
          <!-- <a class="form_link">忘记密码？</a> -->
          <el-button :loading="loading" type="primary" class="form_button button submit"
            @click="handlelogin()">登录</el-button>
        </el-form>
      </div>

      <!-- 按钮切换 -->
      <div class="switch" ref="switchCtn">
        <div class="switch_circle"></div>
        <div class="switch_circle switch_circle-t"></div>
        <div class="switch_container" ref="switchC1">
          <h2 class="switch_title title" style="letter-spacing: 0;">创建账号！</h2>
          <p class="switch_description description">已经有账号了嘛，去登入账号来进入奇妙世界吧！！！</p>
          <button class="switch_button button switch-btn" @click="changeForm">登录账号</button>
        </div>

        <div class="switch_container is-hidden" ref="switchC2">
          <h2 class="switch_title title" style="letter-spacing: 0;">你好，朋友</h2>
          <p class="switch_description description">去注册一个账号，成为尊贵选课系统用户，让我们踏入奇妙的旅途！</p>
          <button class="switch_button button switch-btn" @click="changeForm">注册账号</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { login, register, getInfo, signUp, getDepartments, getMajorsByDepartmentId } from "@/api/user";
export default {
  data() {
    //学工号 验证
    var validateU_Name = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入学号或工号'));
      } else {
        callback();
      }
    };

    // 验证姓名
    var validateN_Name = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入姓名'));
      } else {
        callback();
      }
    };

    // 验证手机号
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入手机号'));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          // 判断手机号是11位数
          if (value.toString().length !== 11) {
            callback(new Error('号码格式不正确'));
          } else {
            callback();
          }
        }
      }, 1000);
    };

    // 验证密码
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };

    // 验证学院
    var validateS_College = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入所在学院'));
      } else {
        callback();
      }
    };

    // 验证专业
    var validatemajorId = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入专业名称'));
      } else {
        callback();
      }
    };

    // 验证角色
    var validateRole = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('选择身份'));
      } else {
        callback();
      }
    };


    return {

      //  注册表单
      ruleForm: {
        username: "", //学/工 号
        nickname: "", //姓名
        password: '', //密码
        phone: "",  //手机号
        departmentId: "", //学院
        majorId: "", //专业
        use_role: '', //角色
        enrTime: '' // 入学年份
      },

      // 教师
      teacher: true,
      department: {}, //学院数据
      major: {}, // 专业数据

      // 校验表单
      rules: {
        username: [
          { validator: validateU_Name, trigger: 'blur' }
        ],
        nickname: [
          { validator: validateN_Name, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],

        phone: [
          { validator: checkPhone, trigger: 'blur' }
        ],
        departmentId: [
          { validator: validateS_College, trigger: 'blur' }
        ],
        majorId: [
          { validator: validatemajorId, trigger: 'blur' }
        ],
        use_role: [
          { validator: validateRole, trigger: 'blur' }
        ]
      },


      // 登录 用户和密码
      user: { username: "", password: "" },
      redirect: undefined,
      loading: false,
    }
  },
  created() {
    this.departmentData();
  },
  mounted() {
    this.changeForm();
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    //登陆方法
    handlelogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.user)
            .then(() => {
              this.$router.push("/");
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
              alert('错误');
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 获取部门数据方法
    departmentData() {
      getDepartments().then(res => {
        this.department = res.data
        // console.log("专业",res);
      })
    },
    // 获取学院数据
    majors() {
      getMajorsByDepartmentId(this.ruleForm.departmentId).then(res => {
        this.major = res.data
      })
      this.ruleForm.majorId = '';  //学院清空
    },


    // 转换按钮方法
    changeForm() {
      // 修改类名
      this.$refs.switchCtn.classList.add("is-gx");
      setTimeout(() => {
        this.$refs.switchCtn.classList.remove("is-gx");
      },);
      this.$refs.switchCtn.classList.toggle("is-txr");
      this.$refs.switchCtn.querySelectorAll(".switch_circle")[0].classList.toggle("is-txr");
      this.$refs.switchCtn.querySelectorAll(".switch_circle")[1].classList.toggle("is-txr");
      this.$refs.switchC1.classList.toggle("is-hidden");
      this.$refs.switchC2.classList.toggle("is-hidden");
      this.$refs.aContainer.classList.toggle("is-txl");
      this.$refs.bContainer.classList.toggle("is-txl");
      this.$refs.bContainer.classList.toggle("is-z");

    },



    // 注册表单提交 按钮方法
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          signUp(this.ruleForm).then(res => {
            if (res.code == 200) {
              this.open2();
              this.resetForm(formName);
              setTimeout(() => {
                this.changeForm();
              }, 500);

            } else if (res.error) {
              this.open4();
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 注册 重置输入框方法
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    //消息提示方法
    open2() {
      this.$message({
        showClose: true,
        message: '恭喜你，注册成功',
        type: 'success'
      });
    },

    open4() {
      this.$message({
        showClose: true,
        message: '很遗憾，注册失败',
        type: 'error'
      });
    }


  },
}



</script>
<style scoped>
* {
  /* 字体无法选中 */
  user-select: none;

  /* border: 1px solid #ccc;  */
}

.box {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
  background-color: #ecf0f3;
  color: #a0a5a8;
}

.shell {
  position: relative;
  width: 1000px;
  min-width: 1000px;
  min-height: 600px;
  height: 600px;
  padding: 25px;
  background-color: #ecf0f3;
  box-shadow: 10px 10px 10px #d1d9e6, -10px -10px 10px #f9f9f9;
  border-radius: 12px;
  overflow: hidden;
}

/* 设置响应式 */
@media (max-width: 1200px) {
  .shell {
    transform: scale(0.7);
  }
}

@media (max-width: 1000px) {
  .shell {
    transform: scale(0.6);
  }
}

@media (max-width: 800px) {
  .shell {
    transform: scale(0.5);
  }
}

@media (max-width: 600px) {
  .shell {
    transform: scale(0.4);
  }
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  width: 600px;
  height: 100%;
  padding: 25px;
  background-color: #ecf0f3;
  transition: 1.25s;
}

.form {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  width: 100%;
  height: 100%;
}

.iconfont {
  margin: 0 5px;
  border: rgba(0, 0, 0, 0.5) 2px solid;
  border-radius: 50%;
  font-size: 25px;
  padding: 3px;
  opacity: 0.5;
  transition: 0.1s;
}

.iconfont:hover {
  opacity: 1;
  transition: 0.15s;
  cursor: pointer;
}

.form_input {
  width: 350px;
  height: 40px;
  margin: 4px 0;
  padding-left: 25px;
  font-size: 13px;
  letter-spacing: 0.15px;
  border: none;
  outline: none;
  background-color: #ecf0f3;
  transition: 0.25s ease;
  border-radius: 8px;
  box-shadow: inset 2px 2px 4px #d1d9e6, inset -2px -2px 4px #f9f9f9;
}

.form_input:focus {
  box-shadow: inset 4px 4px 4px #d1d9e6, inset -4px -4px 4px #f9f9f9;
}

.form_span {
  margin-top: 30px;
  margin-bottom: 12px;
}

.form_link {
  color: #181818;
  font-size: 15px;
  margin-top: 25px;
  border-bottom: 1px solid #a0a5a8;
  line-height: 2;
}

.title {
  font-size: 34px;
  font-weight: 700;
  line-height: 3;
  color: #181818;
  letter-spacing: 10px;
}

.description {
  font-size: 14px;
  letter-spacing: 0.25px;
  text-align: center;
  line-height: 1.6;
}

.button {
  width: 180px;
  height: 50px;
  border-radius: 25px;
  margin-top: 50px;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 1.15px;
  background-color: #4B70E2;
  color: #f9f9f9;
  box-shadow: 8px 8px 16px #d1d9e6, -8px -8px 16px #f9f9f9;
  border: none;
  outline: none;
}

.a-container {
  z-index: 100;
  left: calc(100% - 600px);
}

.b-container {
  left: calc(100% - 600px);
  z-index: 0;
}

.switch {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 400px;
  padding: 50px;
  z-index: 200;
  transition: 1.25s;
  background-color: #ecf0f3;
  overflow: hidden;
  box-shadow: 4px 4px 10px #d1d9e6, -4px -4px 10px #d1d9e6;
}

.switch_circle {
  position: absolute;
  width: 500px;
  height: 500px;
  border-radius: 50%;
  background-color: #ecf0f3;
  box-shadow: inset 8px 8px 12px #b8bec7, inset -8px -8px 12px #fff;
  bottom: -60%;
  left: -60%;
  transition: 1.25s;
}

.switch_circle-t {
  top: -30%;
  left: 60%;
  width: 300px;
  height: 300px;
}

.switch_container {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  position: absolute;
  width: 400px;
  padding: 50px 55px;
  transition: 1.25s;
}

.switch_button {
  cursor: pointer;
}

.switch_button:hover,
.submit:hover {
  box-shadow: 6px 6px 10px #d1d9e6, -6px -6px 10px #f9f9f9;
  transform: scale(0.985);
  transition: 0.25s;
}

.switch_button:active,
.switch_button:focus {
  box-shadow: 2px 2px 6px #d1d9e6, -2px -2px 6px #f9f9f9;
  transform: scale(0.97);
  transition: 0.25s;
}

.is-txr {
  left: calc(100% - 400px);
  transition: 1.25s;
  transform-origin: left;
}

.is-txl {
  left: 0;
  transition: 1.25s;
  transform-origin: right;
}

.is-z {
  z-index: 200;
  transition: 1.25s;
}

.is-hidden {
  visibility: hidden;
  opacity: 0;
  position: absolute;
  transition: 1.25s;
}

.is-gx {
  animation: is-gx 1.25s;
}

@keyframes is-gx {

  0%,
  10%,
  100% {
    width: 400px;
  }

  30%,
  50% {
    width: 500px;
  }
}

/* 注册表单样式 */
.inputs {
  width: 70%;
}

.resetting {
  border: 0;
  ;
  border-radius: 40px;
  color: #ccc;
  background-color: rgba(0, 0, 0, 0);
}
</style>